// packageStore/components/integral-reduce-freight/integral-reduce-freight.js
const componentBehavior = require('../../../js/common/ComponentBehavior')
const componentCallback = require('../../../js/common/componentCallback')

import { calculation } from '../../../js/common/math';
import {
  api, dialog
} from '../../../js/util';

Component({
  behaviors: [componentBehavior, componentCallback],
  /**
   * 组件的属性列表
   */
  properties: {
    Currency: {
      type: String,
      value: '￥'
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    mainColor: api.titleColor || "#e56b14", //主题色
    showComponent: false, // 显示组件
    integralReduceFreight: { // 积分抵扣运费参数
      AccountIntegral: 0, // 用户可用积分
      Deduction: 0, // 最大抵扣金额
      DeductionIntegral: 0, // 此次最大可用积分
      Rate: 0, // 兑换比例
      usingIntegral: 0, // 用户要使用的积分
      originFreight: 0, // 未抵扣时的运费
    },
    usingIntegral: 0, // 输入的积分
    reduceFreight: 0, // 计算后的抵扣金额
  },

  observers: {
    usingIntegral (usingIntegral) {
      let integralReduceFreight = this.data.integralReduceFreight || {}
      let rate = integralReduceFreight.Rate || 0
      let reduceFreight = calculation(usingIntegral, '*', rate)
      this.setData({
        reduceFreight
      })
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    showComponent (data) {
      let integralReduceFreight = data.integralReduceFreight || {}
      this.setData({
        integralReduceFreight: integralReduceFreight,
        usingIntegral: integralReduceFreight.usingIntegral || 0
      })
    },
    /**
     * 点击空白处关闭弹窗
     */
    hideComponent () {
      let {
        componentFail
      } = this.data
      this.setData({
        showComponent: false
      })
      componentFail()
    },
    nothing () {
      return false
    },
    input (e) {
      let integralReduceFreight = this.data.integralReduceFreight || {}
      let value = e.detail.value
      value = parseInt(value)
      if (isNaN(value)) {
        value = 0
      }
      if (value > integralReduceFreight.DeductionIntegral) {
        value = integralReduceFreight.DeductionIntegral
      }
      this.setData({
        usingIntegral: value
      })
    },
    blur (e) {

    },
    /**
     * 不使用积分
     */
    cancel () {
      let {
        integralReduceFreight,
        componentSuccess
      } = this.data
      integralReduceFreight.usingIntegral = 0
      this.setData({
        integralReduceFreight,
        usingIntegral: 0,
        reduceFreight: 0,
      })
      componentSuccess({
        integralReduceFreight,
        reduceFreight: 0,
        Freight: integralReduceFreight.originFreight
      })
      this.setData({
        showComponent: false
      })
    },
    /**
     * 确定
     */
    confirm () {
      let {
        integralReduceFreight,
        componentSuccess,
        usingIntegral,
        reduceFreight
      } = this.data
      integralReduceFreight.usingIntegral = usingIntegral
      // 抵扣后剩余的运费
      let Freight = calculation(integralReduceFreight.originFreight, '-', reduceFreight)
      componentSuccess({
        integralReduceFreight,
        reduceFreight,
        Freight
      })
      this.setData({
        showComponent: false
      })
    },
  }
})